/**
 * Created by yuzhou on 2020/3/29.
 */
//获取
$(document).ready(function () {
    var app = new Vue({
        el: "#whole_box",
        data: {
            all_hot:"",
            store_hot:"",
            category_hot:"",
            lanuage_type:"",

            tab_img1:"/xj/image/search/alled.png",
            tab_img2:"/xj/image/search/store.png",
            tab_img3:"/xj/image/search/pinlei.png",

            search_txt: "搜索更多..",
            hot_search:"",
            all_txt:"",
            brand_txt:"",
            category_txt:"",
            search_txt:"",
            search_txt_i:"",
            hot_search:"",
            store_list:[],
            store_name_list:[],
            store_category_list:[],
            main_pro_txt:"",
            input_search:"",

            search_finish:true,
            page_num:1,

            current_tab:1
        },
        mounted:function(){
            var self = this;
            var type_id = self.getQueryValue("id");
            self.lanuage_type = localStorage.getItem("language");
            if(self.lanuage_type == "cn"){
                self.changeToCn();
            }else{
                self.changeToEn();
            }
            self.getHotSearchTxt();

            $(window).scroll(function () {
                var scrollTop = $(this).scrollTop();
                var scrollHeight = $(document).height();
                var windowHeight = $(this).height();

                if (Math.round(scrollTop + windowHeight) == scrollHeight) {
                    self.page_num = self.page_num+5;
                    if(self.current_tab == 1){
                        self.requestStoreQuickByName(self.input_search);
                    }else if(self.current_tab == 2){
                        self.requestStoreQuickByName(self.input_search);
                    }else if(self.current_tab == 3){
                        self.requestStoreQuickByCategory(self.input_search);
                    }

                }
            });

        },
        methods: {
            getQueryValue:function (a) {
                var query = decodeURI(window.location.search.substring(1));
                var vars = query.split("&");
                for (var i = 0; i < vars.length; i++) {
                    var pair = vars[i].split("=");
                    if (pair[0] == a) { return pair[1]; }
                }
                return null;
            },

            getback:function () {
                window.history.back(-1);
            },

            getHotSearchTxt:function () {
                var  self = this;
                $.ajax({
                    type:"GET",
                    url:"/market/GetAllSearch",
                    data: {
                        language:self.lanuage_type
                    },
                    dataType:"json",
                    success:function(res){
                       self.all_hot = res;
                    }
                });
                $.ajax({
                    type:"GET",
                    url:"/market/GetStoreSearch",
                    data: {
                        language:self.lanuage_type
                    },
                    dataType:"json",
                    success:function(res){
                        self.store_hot = res;
                    }
                });
                $.ajax({
                    type:"GET",
                    url:"/market/GetCategorySearch",
                    data: {
                        language:self.lanuage_type
                    },
                    dataType:"json",
                    success:function(res){
                        self.category_hot = res;
                    }
                });
            },

            changeTab:function (a) {
                var self = this;
                self.current_tab = a;
                if(a == 1){
                    self.tab_img1="/xj/image/search/alled.png";
                    self.tab_img2="/xj/image/search/store.png";
                    self.tab_img3="/xj/image/search/pinlei.png";
                }else if(a == 2){
                    self.tab_img1="/xj/image/search/all.png";
                    self.tab_img2="/xj/image/search/storeed.png";
                    self.tab_img3="/xj/image/search/pinlei.png";
                }else if(a == 3){
                    self.tab_img1="/xj/image/search/all.png";
                    self.tab_img2="/xj/image/search/store.png";
                    self.tab_img3="/xj/image/search/pinleied.png";
                }
            },

            changeToCn:function () {
                var self = this;
                self.hot_search = "热门搜索",
                self.all_txt = "全部";
                self.brand_txt = "商家";
                self.category_txt = "品类";
                self.search_txt = "搜索";
                self.search_txt_i = "输入商家名字，产品类目..";
                self.main_pro_txt = "主营"
            },

            changeToEn:function () {
                var self = this;
                self.hot_search = "Hot Search";
                self.all_txt = "All";
                self.brand_txt = "Brand";
                self.category_txt = "Category";
                self.search_txt = "Search";
                self.search_txt_i = "Enter business name and product category";
                self.main_pro_txt = "Main Products"
            },

            requestStoreQuickByName:function (a) {
                var self = this;
                self.input_search = a;
                $.ajax({
                    type:"GET",
                    url:"/search/getStoreBySearch",
                    data: {
                        page:self.page_num,
                        search_str:a,
                        language:localStorage.getItem("language")
                    },
                    dataType:"json",
                    success:function(res){
                        if(self.page_num > 1){
                            self.store_list = self.store_list.concat(res);
                            self.store_name_list = self.store_name_list.concat(res);
                        }else{
                            self.store_list = res;
                            self.store_name_list = res;
                            self.search_finish = false;
                            self.requestStoreQuickByCategory(a);
                        }

                    }
                });
            },

            requestStoreQuickByCategory:function (a) {
                var self = this;

                self.input_search = a;
                $.ajax({
                    type:"GET",
                    url:"/search/getStoreCategoryBySearch",
                    data: {
                        page:self.page_num,
                        search_str:a,
                        language:localStorage.getItem("language")
                    },
                    dataType:"json",
                    success:function(res){
                        if(self.page_num > 1){
                            self.store_list = self.store_list.concat(res);
                            self.store_category_list = self.store_category_list.concat(res);
                        }else{
                            self.store_list = self.store_list.concat(res);
                            self.store_category_list = res;
                            self.search_finish = false;
                        }

                    }
                });
            },

            search:function () {
                var self = this;

                $.ajax({
                    type:"GET",
                    url:"/search/getStoreBySearch",
                    data: {
                        page:1,
                        search_str:self.input_search,
                        language:localStorage.getItem("language")
                    },
                    dataType:"json",
                    success:function(res){
                        self.store_list = res;
                        self.store_name_list = res;
                        self.search_finish = false;
                    }
                });

                $.ajax({
                    type:"GET",
                    url:"/search/getStoreCategoryBySearch",
                    data: {
                        page:1,
                        search_str:self.input_search,
                        language:localStorage.getItem("language")
                    },
                    dataType:"json",
                    success:function(res){
                        self.store_category_list = res;
                        self.search_finish = false;
                    }
                });
            },

            storDetail:function (a) {
                window.location.href = "/page/redirectStoreDeatil?id="+a;
            },


        },

    });
});